/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameCore;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

/**
 *
 * @author NGUYEN VAN TAM
 */
public class DataTransmission {
    public int ID;
    
    Socket connectSocket;
    
    //input read message form server 
    InputStream is;
    BufferedReader br;

    //output send message to server
    OutputStream os;
    BufferedWriter bw;
    
    public boolean Init()
    {
        try
        {
            is = connectSocket.getInputStream();
            br=new BufferedReader(new InputStreamReader(is));

            os = connectSocket.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));
            
            return true;
        }
        catch(IOException ioex)
        {
            return false;
        }
    }
    
    public String RecieveMessage()
    {
        try
        {
            String message = br.readLine();
            return message;
        }
        catch(IOException io)
        {
            return null;
        }
    }
    
    public boolean SendMessage(String message)
    {
        try
        {
            bw.write(message);
            bw.newLine();
            bw.flush();
            return true;
        }
        catch(IOException io)
        {
            return false;
        }
    }
    
    public boolean Close()
    {
        try
        {
            bw.close();
            br.close();
            connectSocket.close();
            return true;
        }
        catch(IOException io)
        {
            return false;
        }     
    }
}
